package com.metadata_system.api_server.auth_server;

import com.metadata_system.api.annotations.ApiCheckSign;
import com.metadata_system.api.annotations.CheckTokenUser;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.dto.JwtToken;
import com.metadata_system.common.http.request_dto.LoginUserRequest;
import com.metadata_system.common.request.UserLoginLogListParams;
import com.metadata_system.common.service.AuthTokenService;
import com.metadata_system.common.service.UserLoginLogService;
import com.metadata_system.common.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController("authUserController")
@RequestMapping(value = "/api/auth-server/v001/User/")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    AuthTokenService authTokenService;
    @Autowired
    LoginUserRequest loginUserRequest;
    @Autowired
    UserLoginLogService userLoginLogService;


    @RequestMapping(value = "info", method = {RequestMethod.GET, RequestMethod.POST})
    @CheckTokenUser
    @ApiCheckSign
    public ApiResult info() {
        JwtToken tokenUser = this.loginUserRequest.getTokenUser();
        HashMap<String, Object> backMap = new HashMap<>();
        backMap.put("username",tokenUser.getUsername());
        backMap.put("role",tokenUser.getRole());
        return ApiResult.success(backMap);
    }



    @RequestMapping(value = "getLoginLogList", method = {RequestMethod.GET, RequestMethod.POST})
    @CheckTokenUser
    @ApiCheckSign
    public ApiResult getLoginLogList(
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String createdTime,
            @RequestParam(required = false,defaultValue = "1") Integer page,
            @RequestParam(required = false,defaultValue = "50") Integer pageSize,
            @RequestParam(required = false,defaultValue = "id") String sortProp,
            @RequestParam(required = false,defaultValue = "DESC") String sortOrder
    ) {
         return userLoginLogService.getList(
                UserLoginLogListParams.builder()
                        .userId(Integer.valueOf(loginUserRequest.getTokenUser().decodeID()))
                        .role(loginUserRequest.getTokenUser().getRole())
                        .page(page)
                        .pageSize(pageSize)
                        .createdTime(createdTime)
                        .status(status)
                        .sortOrder(sortOrder)
                        .sortProp(sortProp)
                        .build()
        );
    }



    @RequestMapping(value = "changePassword", method = {RequestMethod.GET, RequestMethod.POST})
    @CheckTokenUser
    @ApiCheckSign
    public ApiResult changePassword(HttpServletRequest request) {
        String oldPassword = ServletRequestUtils.getStringParameter(request, "oldPassword", "");
        String password = ServletRequestUtils.getStringParameter(request, "password", "");
        return this.userService.changePassword(this.loginUserRequest.getTokenUser().decodeID(), oldPassword, password);
    }




    @RequestMapping(value = "loginOut", method = {RequestMethod.GET, RequestMethod.POST})
    @CheckTokenUser
    @ApiCheckSign
    public ApiResult loginOut(HttpServletRequest request) {
        LoginUserRequest loadHeaderData = LoginUserRequest.loadHeaderData(request);
        return this.userService.loginOut(loadHeaderData);
    }



    /**
     * @api {post} /v001/User/reflushToken   05.刷新token
     * @apiVersion 0.0.1
     * @apiGroup 3.用户
     * @apiParam {String} reflushToken=eyJhbGciOiJIUzI1NiJ9.eyJ0b2tlbklkIjoiZWJmZThiM2YtMjVmNC...TI1 刷新token
     * @apiSuccessExample {json} 响应成功:
     * {
     *   "message": "",
     *   "code": 0,
     *   "data":"",
     *   "extendInfo":[],
     *   "timeStamp": 0,
     *   "execTime": 0
     * }
     */
    @RequestMapping(value = "reflushToken", method = {RequestMethod.GET, RequestMethod.POST})
    @CheckTokenUser
    @ApiCheckSign
    public ApiResult reflushToken(HttpServletRequest request) {
        // 用户登录信息
        LoginUserRequest userTokenRequest = LoginUserRequest.loadHeaderData(request);
        // 刷新token
        String reflushToken = ServletRequestUtils.getStringParameter(request, "reflushToken", "");
        return this.userService.reflushToken(userTokenRequest, reflushToken);
    }

}
